package com.baijiayun.liveuibase.widgets.toolbar;

import android.content.Context;
import android.view.View;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.base.BaseUIConstant.SelectSrc;
import com.baijiayun.liveuibase.widgets.popupwindow.BaseAutoArrangePopupWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author lizhidong
 */
public abstract class BaseDrawWidthSettingWindow extends BaseAutoArrangePopupWindow {

    public static final float[] pencilPaintSize = new float[]{2f, 4f, 6f, 8f};

    public static final float[] markerPaintSize = new float[]{8f, 12f, 14f, 24f};

    private final int widthGroupId = 1;
    private final int colorGroupId = 2;

    public BaseDrawWidthSettingWindow(boolean showMarker, Context context) {
        super(context);

        final float[] targetSizes = showMarker ? markerPaintSize : pencilPaintSize;
        int[] markerSelectDrawable = new int[]{
                R.drawable.base_ic_marker_width_1_press,
                R.drawable.base_ic_marker_width_2_press,
                R.drawable.base_ic_marker_width_3_press,
                R.drawable.base_ic_marker_width_4_press
        };
        int[] pencilSelectDrawable = new int[]{
                R.drawable.base_ic_brush_width_1_press,
                R.drawable.base_ic_brush_width_2_press,
                R.drawable.base_ic_brush_width_3_press,
                R.drawable.base_ic_brush_width_4_press,
        };
        final int[] targetSelectDrawable = showMarker ? markerSelectDrawable : pencilSelectDrawable;
        int[] markerUnSelectDrawable = new int[]{
                R.drawable.base_ic_marker_width_1_unpress,
                R.drawable.base_ic_marker_width_2_unpress,
                R.drawable.base_ic_marker_width_3_unpress,
                R.drawable.base_ic_marker_width_4_unpress
        };
        int[] pencilUnSelectDrawable = new int[]{
                R.drawable.base_ic_brush_width_1_unpress,
                R.drawable.base_ic_brush_width_2_unpress,
                R.drawable.base_ic_brush_width_3_unpress,
                R.drawable.base_ic_brush_width_4_unpress
        };
        final int[] targetUnSelectDrawable = showMarker ? markerUnSelectDrawable : pencilUnSelectDrawable;

        List<ResSet> resSets = new ArrayList<>();
        for (int i = 0; i < pencilPaintSize.length; i++) {
            final int index = i;
            resSets.add(new ResSet(getDrawable(targetUnSelectDrawable[i]),
                    getDrawable(targetSelectDrawable[i], true)
                    , widthGroupId, "", v -> onChangeWidth(new WidthSelectData(
                    showMarker ? SelectSrc.Marker : SelectSrc.Brush, targetSizes[index]))));
        }

        setResSetsMergeColors(resSets, colorGroupId, color -> onChangeColor(new ColorSelectData(
                showMarker ? SelectSrc.Marker : SelectSrc.Brush, color)));
        setSelectMode(GROUP_SINGLE);
        setContentPadding(0, 0, 0, 4);

        createView();
        updateCheckedIndex(1);
    }

    /**
     * 宽度变更
     */
    protected abstract void onChangeWidth(WidthSelectData widthSelectData);

    protected abstract void onChangeColor(ColorSelectData colorSelectData);

    @Override
    protected void initViewDataHashMap() {
        HashMap<Integer, BaseAutoArrangePopupWindow.GroupViewData> groupViewDataHashMap = new HashMap<>();
        groupViewDataHashMap.put(widthGroupId,
                new BaseAutoArrangePopupWindow.GroupViewData(context, 130, 35, COMMON_CUSTOM_ITEM_SIZE
                        , COMMON_CUSTOM_ITEM_SIZE, 4));
        groupViewDataHashMap.put(colorGroupId,
                new BaseAutoArrangePopupWindow.GroupViewData(context, 130, 35, COMMON_CUSTOM_COLORS_ITEM_SIZE
                        , COMMON_CUSTOM_COLORS_ITEM_SIZE, 4));
        this.viewDataHashMap = groupViewDataHashMap;
    }

    @Override
    public void show(View anchor) {
        showWithViewOfDirection(anchor, LEFT_BOTTOM);
    }
}
